package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.model.BaseCategoryTrademark;
import com.atguigu.gmall.product.mapper.BaseCategoryTrademarkMapper;
import com.atguigu.gmall.product.model.BaseTrademark;
import com.atguigu.gmall.product.model.CategoryTrademarkVo;
import com.atguigu.gmall.product.service.BaseCategoryTrademarkService;
import com.atguigu.gmall.product.service.BaseTrademarkService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 分类品牌中间表 业务实现类
 *
 * @author atguigu
 * @since 2023-06-07
 */
@Service
public class BaseCategoryTrademarkServiceImpl extends ServiceImpl<BaseCategoryTrademarkMapper, BaseCategoryTrademark> implements BaseCategoryTrademarkService {

    @Resource
    private BaseTrademarkService baseTrademarkService;

    /**
     * 查询指定分类下包含品牌列表
     *
     * @param category3Id
     * @return
     */
    @Override
    public List<BaseTrademark> getTrademarkList(Long category3Id) {
        LambdaQueryWrapper<BaseCategoryTrademark> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseCategoryTrademark::getCategory3Id, category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarkList = this.list(queryWrapper);
        if (!CollectionUtils.isEmpty(baseCategoryTrademarkList)) {
            List<Long> idsList = baseCategoryTrademarkList.stream().map(BaseCategoryTrademark::getTrademarkId)
                    .collect(Collectors.toList());
            return baseTrademarkService.listByIds(idsList);
        }
        return null;
    }

    /**
     * 查询指定分类下未关联品牌列表(可选品牌列表)
     *
     * @param category3Id
     * @return
     */
    @Override
    public List<BaseTrademark> getCurrentTrademarkList(Long category3Id) {
        //获取持久层mapper 调用mapper自定义方法 编写动态SQL
        return this.baseMapper.getCurrentTrademarkList(category3Id);
    }

    /**
     * 保存分类品牌关联
     *
     * @param categoryTrademarkVo
     * @return
     */
    @Override
    public void saveBaseCategoryTrademark(CategoryTrademarkVo categoryTrademarkVo) {
        // 1.获得Vo对象的id的集合  品牌ID集合
        List<Long> trademarkIdList = categoryTrademarkVo.getTrademarkIdList();
        if (!CollectionUtils.isEmpty(trademarkIdList)) {
            List<BaseCategoryTrademark> baseCategoryTrademarkList = trademarkIdList.stream()
                    .map(trademarkId -> {
                BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
                baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
                baseCategoryTrademark.setTrademarkId(trademarkId);
                return baseCategoryTrademark;
            }).collect(Collectors.toList());
            // 2.根据id查trademarkIdList 最后走批量新增
            this.saveBatch(baseCategoryTrademarkList);
        }
    }
    /**
     * 删除指定分类下品牌
     * @param category3Id
     * @param trademarkId
     * @return
     */
    @Override
    public void removeCategoryTrademark(Long category3Id, Long trademarkId) {
        LambdaQueryWrapper<BaseCategoryTrademark> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseCategoryTrademark::getTrademarkId,trademarkId);
        queryWrapper.eq(BaseCategoryTrademark::getCategory3Id,category3Id);
        this.remove(queryWrapper);
    }
}
